home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / pycentral-data / pyversions.pyc (.txt) < prev   
Python Compiled Bytecode  |  2008-10-29  |  13KB  |  463 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import os
  5. import re
  6. import string
  7. import sys
  8.  
  9. try:
  10.     SetType = set
  11. except NameError:
  12.     import sets
  13.     SetType = sets.Set
  14.     set = sets.Set
  15.  
  16.  
  17. class PyCentralEmptyValueError(ValueError):
  18.     '''Python Central Value Error'''
  19.     pass
  20.  
  21. _defaults = None
  22.  
  23. def read_default(name = None):
  24.     global _defaults
  25.     SafeConfigParser = SafeConfigParser
  26.     NoOptionError = NoOptionError
  27.     import ConfigParser
  28.     if not _defaults:
  29.         if os.path.exists('/usr/share/python/debian_defaults'):
  30.             config = SafeConfigParser()
  31.             config.readfp(file('/usr/share/python/debian_defaults'))
  32.             _defaults = config
  33.         else:
  34.             raise ValueError
  35.     
  36.     if _defaults and name:
  37.         
  38.         try:
  39.             value = _defaults.get('DEFAULT', name)
  40.         except NoOptionError:
  41.             raise ValueError
  42.  
  43.         return value
  44.     
  45.  
  46.  
  47. def parse_versions(vstring):
  48.     if vstring.lower().startswith('python:versions='):
  49.         vstring = string.split(vstring, '=', maxsplit = 1)[1]
  50.     
  51.     import operator as operator
  52.     operators = {
  53.         None: operator.eq,
  54.         '=': operator.eq,
  55.         '>=': operator.ge,
  56.         '<=': operator.le,
  57.         '<<': operator.lt }
  58.     vinfo = { }
  59.     exact_versions = set([])
  60.     version_range = set(supported_versions(version_only = True) + old_versions(version_only = True))
  61.     relop_seen = False
  62.     for field in vstring.split(','):
  63.         field = field.strip()
  64.         if field == 'all':
  65.             vinfo['all'] = 'all'
  66.             continue
  67.         
  68.         if field in ('current', 'current_ext'):
  69.             vinfo['current'] = field
  70.             continue
  71.         
  72.         vinfo.setdefault('versions', set())
  73.         ve = re.compile('(>=|<=|<<|=)? *(\\d\\.\\d)$')
  74.         m = ve.match(field)
  75.         
  76.         try:
  77.             op = m.group(1)
  78.             v = m.group(2)
  79.             if op in (None, '='):
  80.                 exact_versions.add(v)
  81.             else:
  82.                 relop_seen = True
  83.                 filtop = operators[op]
  84.                 version_range = _[1]
  85.         continue
  86.         except Exception:
  87.             raise ValueError, 'error parsing Python-Version attribute'
  88.             continue
  89.         
  90.  
  91.     
  92.     if 'versions' in vinfo:
  93.         vinfo['versions'] = exact_versions
  94.         if relop_seen:
  95.             vinfo['versions'] = exact_versions.union(version_range)
  96.         
  97.     
  98.     return vinfo
  99.  
  100. _old_versions = None
  101.  
  102. def old_versions(version_only = False):
  103.     global _old_versions
  104.     if not _old_versions:
  105.         
  106.         try:
  107.             value = read_default('old-versions')
  108.             _old_versions = [ s.strip() for s in value.split(',') ]
  109.         except ValueError:
  110.             _old_versions = []
  111.         except:
  112.             None<EXCEPTION MATCH>ValueError
  113.         
  114.  
  115.     None<EXCEPTION MATCH>ValueError
  116.  
  117. _unsupported_versions = None
  118.  
  119. def unsupported_versions(version_only = False):
  120.     global _unsupported_versions
  121.     if not _unsupported_versions:
  122.         
  123.         try:
  124.             value = read_default('unsupported-versions')
  125.             _unsupported_versions = [ s.strip() for s in value.split(',') ]
  126.         except ValueError:
  127.             _unsupported_versions = []
  128.         except:
  129.             None<EXCEPTION MATCH>ValueError
  130.         
  131.  
  132.     None<EXCEPTION MATCH>ValueError
  133.  
  134. _supported_versions = None
  135.  
  136. def supported_versions(version_only = False):
  137.     global _supported_versions
  138.     if not _supported_versions:
  139.         
  140.         try:
  141.             value = read_default('supported-versions')
  142.             _supported_versions = [ s.strip() for s in value.split(',') ]
  143.         except ValueError:
  144.             cmd = [
  145.                 '/usr/bin/apt-cache',
  146.                 '--no-all-versions',
  147.                 'show',
  148.                 'python-all']
  149.             
  150.             try:
  151.                 import subprocess as subprocess
  152.                 p = subprocess.Popen(cmd, bufsize = 1, shell = False, stdout = subprocess.PIPE)
  153.                 fd = p.stdout
  154.             except ImportError:
  155.                 fd = os.popen(' '.join(cmd))
  156.  
  157.             depends = None
  158.             for line in fd:
  159.                 if line.startswith('Depends:'):
  160.                     depends = line.split(':', 1)[1].strip().split(',')
  161.                     continue
  162.             
  163.             fd.close()
  164.             if not _supported_versions:
  165.                 _supported_versions = [
  166.                     'python2.4',
  167.                     'python2.5']
  168.             
  169.         except:
  170.             _supported_versions
  171.         
  172.  
  173.     None if depends else []
  174.  
  175. _default_version = None
  176.  
  177. def default_version(version_only = False):
  178.     global _default_version
  179.     if not _default_version:
  180.         
  181.         try:
  182.             _default_version = link = os.readlink('/usr/bin/python')
  183.         except OSError:
  184.             _default_version = None
  185.             
  186.             try:
  187.                 cmd = [
  188.                     '/usr/bin/python',
  189.                     '-c',
  190.                     'import sys; print sys.version[:3]']
  191.                 import subprocess
  192.                 p = subprocess.Popen(cmd, bufsize = 1, shell = False, stdout = subprocess.PIPE)
  193.                 fd = p.stdout
  194.             except ImportError:
  195.                 fd = os.popen("/usr/bin/python -c 'import sys; print sys.version[:3]'")
  196.  
  197.             line = fd.readline().strip()
  198.             fd.close()
  199.             if re.match('\\d\\.\\d$', line):
  200.                 _default_version = 'python' + line
  201.             
  202.         except:
  203.             re.match('\\d\\.\\d$', line)
  204.  
  205.         
  206.         try:
  207.             debian_default = read_default('default-version')
  208.         except ValueError:
  209.             debian_default = 'python2.4'
  210.  
  211.         if _default_version not in (debian_default, os.path.join('/usr/bin', debian_default)):
  212.             raise ValueError, '/usr/bin/python does not match the python default version. It must be reset to point to %s' % debian_default
  213.         
  214.         _default_version = debian_default
  215.     
  216.     if version_only:
  217.         return _default_version[6:]
  218.     else:
  219.         return _default_version
  220.  
  221.  
  222. def requested_versions(vstring, version_only = False):
  223.     versions = None
  224.     vinfo = parse_versions(vstring)
  225.     supported = supported_versions(version_only = True)
  226.     if len(vinfo) == 1:
  227.         if 'all' in vinfo:
  228.             versions = supported
  229.         elif 'current' in vinfo:
  230.             versions = [
  231.                 default_version(version_only = True)]
  232.         else:
  233.             versions = vinfo['versions'].intersection(supported)
  234.     elif 'all' in vinfo and 'current' in vinfo:
  235.         raise ValueError, "both `current' and `all' in version string"
  236.     elif 'all' in vinfo:
  237.         versions = versions = vinfo['versions'].intersection(supported)
  238.     elif 'current' in vinfo:
  239.         current = default_version(version_only = True)
  240.         if current not in vinfo['versions']:
  241.             raise ValueError, "`current' version not in supported versions"
  242.         
  243.         versions = [
  244.             current]
  245.     else:
  246.         raise ValueError, 'error in version string'
  247.     if not versions:
  248.         raise PyCentralEmptyValueError, 'empty set of versions'
  249.     
  250.  
  251.  
  252. def requested_versions_for_runtime(vstring, version_only = False):
  253.     versions = None
  254.     vinfo = parse_versions(vstring)
  255.     old = old_versions(version_only = True)
  256.     unsupported = unsupported_versions(version_only = True)
  257.     supported = supported_versions(version_only = True)
  258.     supported.extend(old)
  259.     if len(vinfo) == 1:
  260.         if 'all' in vinfo:
  261.             versions = supported
  262.         elif 'current' in vinfo:
  263.             versions = [
  264.                 default_version(version_only = True)]
  265.         else:
  266.             versions = vinfo['versions'].intersection(supported)
  267.     elif 'all' in vinfo and 'current' in vinfo:
  268.         raise ValueError, "both `current' and `all' in version string"
  269.     elif 'all' in vinfo:
  270.         versions = versions = vinfo['versions'].intersection(supported)
  271.     elif 'current' in vinfo:
  272.         current = default_version(version_only = True)
  273.         if current not in vinfo['versions']:
  274.             raise ValueError, "`current' version not in supported versions"
  275.         
  276.         versions = [
  277.             current]
  278.     else:
  279.         raise ValueError, 'error in version string'
  280.     if not versions:
  281.         raise PyCentralEmptyValueError, 'empty set of versions'
  282.     
  283.  
  284.  
  285. def installed_versions(version_only = False):
  286.     import glob as glob
  287.     supported = supported_versions()
  288.     versions = _[1]
  289.     versions.sort()
  290.  
  291.  
  292. class ControlFileValueError(ValueError):
  293.     pass
  294.  
  295.  
  296. class MissingVersionValueError(ValueError):
  297.     pass
  298.  
  299.  
  300. def extract_pyversion_attribute(fn, pkg):
  301.     '''read the debian/control file, extract the XS-Python-Version
  302.     field; check that XB-Python-Version exists for the package.'''
  303.     version = None
  304.     sversion = None
  305.     section = None
  306.     for line in file(fn):
  307.         line = line.strip()
  308.         if line == '':
  309.             if pkg == 'Source':
  310.                 break
  311.             
  312.             section = None
  313.             continue
  314.         if line.startswith('Source:'):
  315.             section = 'Source'
  316.             continue
  317.         if line.startswith('Package: ' + pkg):
  318.             section = pkg
  319.             continue
  320.         None if line.startswith('XS-Python-Version:') else section == pkg
  321.     
  322.     if section == None:
  323.         raise ControlFileValueError, 'not a control file'
  324.     
  325.     if pkg == 'Source':
  326.         if sversion == None:
  327.             raise MissingVersionValueError, 'missing XS-Python-Version in control file'
  328.         
  329.         return sversion
  330.     
  331.     if version == None:
  332.         raise MissingVersionValueError, 'missing XB-Python-Version for package `%s' % pkg
  333.     
  334.     return version
  335.  
  336.  
  337. def version_cmp(ver1, ver2):
  338.     v1 = [ int(i) for i in ver1.split('.') ]
  339.     v2 = [ int(i) for i in ver2.split('.') ]
  340.     return cmp(v1, v2)
  341.  
  342.  
  343. def requested_versions_bis(vstring, version_only = False):
  344.     versions = []
  345.     py_supported_short = supported_versions(version_only = True)
  346.     for item in vstring.split(','):
  347.         v = item.split('-')
  348.         if len(v) > 1:
  349.             if not v[0]:
  350.                 v[0] = py_supported_short[0]
  351.             
  352.             if not v[1]:
  353.                 v[1] = py_supported_short[-1]
  354.             
  355.             for ver in py_supported_short:
  356.                 
  357.                 try:
  358.                     if version_cmp(ver, v[0]) >= 0 and version_cmp(ver, v[1]) <= 0:
  359.                         versions.append(ver)
  360.                 continue
  361.                 except ValueError:
  362.                     continue
  363.                 
  364.  
  365.             
  366.         None<EXCEPTION MATCH>ValueError
  367.         if v[0] in py_supported_short:
  368.             versions.append(v[0])
  369.             continue
  370.     
  371.     versions.sort(version_cmp)
  372.     if not versions:
  373.         raise ValueError, 'empty set of versions'
  374.     
  375.     return versions
  376.  
  377.  
  378. def extract_pyversion_attribute_bis(fn):
  379.     vstring = file(fn).readline().rstrip('\n')
  380.     return vstring
  381.  
  382.  
  383. def main():
  384.     OptionParser = OptionParser
  385.     import optparse
  386.     usage = '[-v] [-h] [-d|--default] [-s|--supported] [-i|--installed] [-r|--requested <version string>|<control file>]'
  387.     parser = OptionParser(usage = usage)
  388.     parser.add_option('-d', '--default', help = 'print the default python version', action = 'store_true', dest = 'default')
  389.     parser.add_option('-s', '--supported', help = 'print the supported python versions', action = 'store_true', dest = 'supported')
  390.     parser.add_option('-r', '--requested', help = 'print the python versions requested by a build; the argument is either the name of a control file or the value of the XS-Python-Version attribute', action = 'store_true', dest = 'requested')
  391.     parser.add_option('-i', '--installed', help = 'print the installed supported python versions', action = 'store_true', dest = 'installed')
  392.     parser.add_option('-v', '--version', help = 'print just the version number(s)', default = False, action = 'store_true', dest = 'version_only')
  393.     (opts, args) = parser.parse_args()
  394.     program = os.path.basename(sys.argv[0])
  395.     if opts.default and len(args) == 0:
  396.         
  397.         try:
  398.             print default_version(opts.version_only)
  399.         except ValueError:
  400.             msg = None
  401.             print '%s:' % program, msg
  402.             sys.exit(1)
  403.         except:
  404.             None<EXCEPTION MATCH>ValueError
  405.         
  406.  
  407.     None<EXCEPTION MATCH>ValueError
  408.     if opts.supported and len(args) == 0:
  409.         print ' '.join(supported_versions(opts.version_only))
  410.     elif opts.installed and len(args) == 0:
  411.         print ' '.join(installed_versions(opts.version_only))
  412.     elif opts.requested and len(args) <= 1:
  413.         if len(args) == 0:
  414.             versions = 'debian/control'
  415.         else:
  416.             versions = args[0]
  417.         
  418.         try:
  419.             if os.path.isfile(versions):
  420.                 fn = versions
  421.                 
  422.                 try:
  423.                     vstring = extract_pyversion_attribute(fn, 'Source')
  424.                     vs = requested_versions(vstring, opts.version_only)
  425.                 except ControlFileValueError:
  426.                     sys.stderr.write('%s: not a control file: %s, ' % (program, fn))
  427.                     sys.exit(1)
  428.                 except MissingVersionValueError:
  429.                     fn = os.path.join(os.path.dirname(fn), 'pyversions')
  430.                     sys.stderr.write('%s: missing XS-Python-Version in control file, fall back to %s\n' % (program, fn))
  431.                     
  432.                     try:
  433.                         vstring = extract_pyversion_attribute_bis(fn)
  434.                         vs = requested_versions_bis(vstring, opts.version_only)
  435.                     except IOError:
  436.                         sys.stderr.write('%s: missing debian/pyversions file, fall back to supported versions\n' % program)
  437.                         vs = supported_versions(opts.version_only)
  438.                     except:
  439.                         None<EXCEPTION MATCH>IOError
  440.                     
  441.  
  442.                     None<EXCEPTION MATCH>IOError
  443.                 
  444.  
  445.             None<EXCEPTION MATCH>ControlFileValueError
  446.             vs = requested_versions(versions, opts.version_only)
  447.             print ' '.join(vs)
  448.         except ValueError:
  449.             msg = None
  450.             sys.stderr.write('%s: %s\n' % (program, msg))
  451.             sys.exit(1)
  452.         except:
  453.             None<EXCEPTION MATCH>ValueError
  454.         
  455.  
  456.     None<EXCEPTION MATCH>ValueError
  457.     sys.stderr.write('usage: %s %s\n' % (program, usage))
  458.     sys.exit(1)
  459.  
  460. if __name__ == '__main__':
  461.     main()
  462.  
  463.